package com.yellowpage.nsp.listener;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;

import com.yellowpage.nsp.data.NspLogger;

public class FetchNSPLoggerTask extends TimerTask{
	
	private final static Properties properties = new Properties();
	private static Log logger = LogFactory.getLog(FetchNSPLoggerTask.class);
	
	String nsp_db_driver;
	String nsp_db_url;
	String nsp_db_username;
	String nsp_db_password;
	
	String driverClassName;
	String username;
	String password;
	String url;
	
	public FetchNSPLoggerTask(){
		try {
			//properties.load(new FileInputStream("jdbc.properties"));
			
			properties.load(this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"));  

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		nsp_db_driver = properties.getProperty("nsp_db_driver");
		nsp_db_url = properties.getProperty("nsp_db_url");
		nsp_db_username = properties.getProperty("nsp_db_username");
		nsp_db_password = properties.getProperty("nsp_db_password");
		
		
		driverClassName = properties.getProperty("jdbc.driverClassName");
		username = properties.getProperty("jdbc.username");
		password = properties.getProperty("jdbc.password");
		url = properties.getProperty("jdbc.url");
		
	}
	
	
	
	public void run() {
        Connection omcConn = null;
        Connection nspConn = null;
        PreparedStatement omcPs = null;
        PreparedStatement nspPs = null;
        try {

        	omcConn = getOMCConnection();  
        	nspConn = getNSPConnection();  
        	
        	omcPs =  omcConn.prepareStatement(
        			"SELECT max(ID) as MAXID FROM NSP_LOGGER a WHERE 1=1 ");
        	
        	ResultSet omcRs = omcPs.executeQuery();        	
        	omcRs.next();
        	int startId = omcRs.getInt(1);
        	       

        	nspPs = nspConn.prepareStatement(
        			"select id, logid, requestip, requesttype, requestinfo, requesttime," +
                    "       responsecode, responsedata, responseinfo " +
                    "  from logger where id > " + startId);
        	
           ResultSet re = nspPs.executeQuery();          
        	
           
           omcPs =  omcConn.prepareStatement(
			" insert into NSP_LOGGER a (ID, LOG_ID, REQUEST_TIME, REQUEST_INFO, REQUEST_TYPE, REQUEST_IP," +
			"                            RESPONSE_CODE,RESPONSE_DATA, RESPONSE_INFO) " +
			"      values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
                      
        	while (re.next()) {
        		
        		omcPs.setInt(1, re.getInt(1));
        		omcPs.setString(2, re.getString(2));
        		omcPs.setString(3, re.getString(3));
        		omcPs.setString(4, re.getString(4));
        		omcPs.setString(5, re.getString(5));
        		omcPs.setString(6, re.getString(6));
        		omcPs.setString(7, re.getString(7));
        		omcPs.setString(8, re.getString(8));
        		omcPs.setString(9, re.getString(9)); 	
        		
        		omcPs.addBatch();	
			}
        	
        	omcPs.executeBatch();
        	
            
        	re.close();
        	
        	omcPs.close();
        	nspPs.close();
        	
        	omcConn.close();
            nspConn.close();

        } catch (Exception e) {
            logger.error(e);
        } finally {
            try{
                if (omcPs != null)	omcPs.close();
                if (nspPs != null)	nspPs.close();
                if (omcConn != null) omcConn.close();
                if (nspConn != null) nspConn.close();
            }catch(Exception e){
                logger.error(e);
            }
        }
    }

    private Connection getOMCConnection(){
        try {
            
           Class.forName(driverClassName);


            //登录Oracle服务器的信息          
            Connection connection = DriverManager.getConnection(url, username, password);

            return connection;

        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }
    
    private Connection getNSPConnection(){
        try {
            //logger.info(serverInfo + ":" + SessionCount.getCount());

            Class.forName(nsp_db_driver);
        
            Connection connection = DriverManager.getConnection(nsp_db_url, nsp_db_username, nsp_db_password);

            return connection;

        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

}
